﻿
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using PloobsEngine.Utils;

namespace PloobsEngine.Particle3D
{
    public class PlanarEmiter : GeneralEmiter
    {
        public PlanarEmiter(Vector3 pos)
        {
            //particlesPos = pos;

            p1 = pos;
            p2 = pos + new Vector3(1000, 0, 0);
            p3 = p2 + new Vector3(0, 0, 1000);
            p4 = p3 - new Vector3(1000, 0, 0);



        }

        public PlanarEmiter(Vector3 p1, Vector3 p2, Vector3 p3, Vector3 p4)
        {
            this.p1 = p1;
            this.p2 = p2;
            this.p3 = p3;
            this.p4 = p4;
        
        }

     
        /// <summary>
        /// Defining the plane
        /// </summary>
        Vector3 p1, p2, p3,p4;

        #region IEmiter Members

        public override string[] ParticleSystemUsed()
        {
            return new string[] {"WaterDropParticleSystem" };
        }


        /// <summary>
        /// Helper for updating the fire effect.
        /// </summary>
        void UpdateDrop()
        {
            const int fireParticlesPerFrame = 5;

            // Create a number of fire particles, randomly positioned around a circle.
            for (int i = 0; i < fireParticlesPerFrame; i++)
            {
                ParticlesSystems["WaterDropParticleSystem"].AddParticle(RandomPointOnPlane() , Vector3.Zero);
            }

           
        }


        bool firsttriangle = true;

        /// <summary>
        /// Helper used by the UpdateFire method. Chooses a random location
        /// around a circle, at which a fire particle will be created.
        /// </summary>
        Vector3 RandomPointOnPlane()
        {
            //const float radius = 30;
            //const float height = 40;

            //double angle = StaticRandom.Random() * Math.PI * 2;
            Vector3 randompos;

            if (firsttriangle)
            {
                randompos = StaticRandom.PickRandomPoint(p1, p2, p3);
                firsttriangle = false;
            }
            else
            {
                randompos = StaticRandom.PickRandomPoint(p3, p4, p1);
                firsttriangle = true;
            }
          
            return randompos;
            //float x = (float)Math.Cos(angle);
            //float y = (float)Math.Sin(angle);

            //return new Vector3(x * radius, y * radius + height, 0);
        }



        public bool isActive()
        {
            return true;
        }

        #endregion

        public override void UpdateEmiter(GameTime gt)
        {
            UpdateDrop();
        }

    }
}
